package idiolect.core;

import idiolect.core.Constants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class SymbolsPanel extends JPanel {

    public SymbolsPanel(ActionListener listener) {
        int len = Constants.TOTAL.length();
        int side = (int) Math.floor(Math.sqrt(len));
        setLayout(new GridLayout(side, side, 3, 3));
        outer:
        for (int row = 0; row < side; row++) {
            for (int column = 0; column < side; column++) {
                int pos = row * side + column;
                if (pos >= len)
                    break outer;
                char ch = Constants.TOTAL.charAt(pos);
                JButton b = new JButton(String.valueOf(ch));
                b.setActionCommand(String.valueOf(ch));
                b.addActionListener(listener);
                b.setMargin(new Insets(1, 1, 1, 1));
                b.setFocusPainted(false);
                b.setFont(b.getFont().deriveFont(16F));
                Color c = Color.PINK;
                if (Constants.CONSONANTS.indexOf(ch) != -1)
                    c = Color.CYAN;
                if (Constants.DIACRITICS_AND_HELPERS.indexOf(ch) != -1)
                    c = Color.LIGHT_GRAY;
                b.setBackground(c);
                add(b);
            }
        }
    }

    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i<getComponentCount();i++)
            getComponent(i).setEnabled(enabled);
    }
}
